const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}


const formatDate = date => {
  let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
  let year = date.getFullYear();
  let month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
  let day = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
  let week = show_day[new Date(date).getDay()];
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()


  // return `${year}-${month}-${day}.${week}`;
  return {
    "date": `${year}-${month}-${day}`,
    "time": [hour, minute, second].map(formatNumber).join(':'),
    "week": `${week}`,
    "date_week": `${year}-${month}-${day}.${week}`
  };
}

let currentFirstDate;
//获取一周的日期,返回array,本周，上周，下周
/**
 * 本周: util.setDate(new Date); 上周:util.setDate(new Date)
 * @param {} date 
 */
function getWeekArr(date, n) {
  console.log(currentFirstDate)

  switch (n) {
    case 0:
      return setDate(date)
    case -7:
      return setDate(addDate(currentFirstDate,-7))
    case 7:
      return setDate(addDate(currentFirstDate,7))
    default:
      return
  }
}

function setDate(date) {
  var week = date.getDay() - 1;
  date = addDate(date, week * -1);
  currentFirstDate = new Date(date)
  console.log(currentFirstDate)

  const arr = []
  for (let i = 0; i < 7; i++) {
    const d = formatDate(i == 0 ? date : addDate(date, 1))
    arr.push(d);
  }

  return arr;
}

function addDate(date, n) {
  date.setDate(date.getDate() + n);
  return date;
}



//转换天数
function checkDay(startTime, endTime) {
  //日期格式化
  // var start_date = new Date(startTime.replace(/-/g, "/"));
  // var end_date = new Date(endTime.replace(/-/g, "/"));
  var start_date = new Date(startTime);
  var end_date = new Date(endTime);
  //转成毫秒数，两个日期相减
  let ms = end_date.getTime() - start_date.getTime() ;
  //转换成天数
  let day = parseInt(ms / (1000 * 60 * 60 * 24));
  let hour = parseInt(ms / (1000 * 60 * 60));
  //do something
  // console.log("day = ", day);
  return {
    day:day,
    hour: hour
  };
}


/***弹窗提示
@params title 提示文字 time 打开时间秒 icon 显示图标[none,success]
*/
function tips(title = '', time = 1500, icon = 'none') {
  wx.showToast({
    title: title,
    time: time,
    icon: icon
  })
}

/***弹窗确认提示
@params title 标题 content 内容 callback 返回函数
*/
function confirm(content = '', callback) {
  wx.showModal({
    title: '温馨提示',
    content: content,
    success: function (res) {
      callback(res);
    }
  })
}

/***云函数请求
@params name 云函数名称 data 参数 {action:'请求方法名',map:'查询参数',params:'参数'} callback 返回函数
*/
function coludRequest(name, data, callback) {
  wx.showLoading({
    title: ''
  })
  wx.cloud.callFunction({
    name: name,
    data: data,
    success: res => {
      wx.hideLoading();
      callback(res);
    },
    fail: err => {
      console.log(err);
      wx.hideLoading();
    }
  })
}

module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  tips: tips,
  confirm: confirm,
  coludRequest: coludRequest,
  checkDay: checkDay,
  getWeekArr: getWeekArr
}